package com.yycx.module.bus.provider.handler.haiying.tasktodo.settle;

import com.yycx.bpm.provider.handler.TaskCompleteHandler;
import com.yycx.bpm.provider.model.TaskRequest;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.security.OpenUser;
import com.yycx.common.utils.DateUtils;
import com.yycx.module.bus.client.entity.*;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.provider.service.*;
import com.yycx.module.bus.provider.service.hy.HyOrderDetailService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 获取任务表单数据
 *
 * @author zyf
 */
@Component
public class HySettlePayFormTaskDataHandler implements TaskCompleteHandler {

    @Autowired
    private BusPaymentService paymentService;

    @Autowired
    private BusSalemanBankService salemanBankService;
    @Autowired
    private BusSalemanService salemanService;
    @Autowired
    private HyOrderDetailService orderDetailService;


    @Override
    public ResultBody prepare(TaskRequest taskRequest, ProcessInstance processInstance, Task task, Map<String, Object> completeVariables, OpenUser user) {
        String businessKey = processInstance.getBusinessKey();

        return ResultBody.ok();
    }

    @Override
    public Map<String, Object> initTaskData(ProcessInstance processInstance, Task task, Map<String, Object> variables) {
        Map<String, Object> ret = new HashMap<>(5);
        String businessKey = processInstance.getBusinessKey();
        BusPayment payment = paymentService.getById(businessKey);
        if (FlymeUtils.isNotEmpty(payment)) {
            String amountType = payment.getAmountType();
            Long bankId = payment.getBankId();
            String bankName = "";
            String bankNo = "";
            String userName = "";

            //市场结算或者退市场保证金
            if (AmountTypeEnum.Settle.getCode().equals(amountType)) {
                BusSalemanBank salemanBank = salemanBankService.getById(bankId);
                if (FlymeUtils.isNotEmpty(salemanBank)) {
                    Long saleManId = salemanBank.getSaleManId();
                    BusSaleman saleman = salemanService.getById(saleManId);
                    bankName = salemanBank.getBankName();
                    bankNo = salemanBank.getBankNo();
                    userName = saleman.getSaleManName();
                }
            }

            ret.put("bankName", bankName);
            ret.put("bankNo", bankNo);
            ret.put("paymentId", payment.getPaymentId());
            ret.put("userName", userName);
            ret.put("payAmount", payment.getAmount());

        }
        return ret;
    }

    /**
     * 表单提交后业务处理
     *
     * @param taskRequest
     * @param processInstance
     * @param task
     * @param completeVariables
     */
    @Override
    public void completeAfter(TaskRequest taskRequest, ProcessInstance processInstance, Task task, Map<String, Object> completeVariables, OpenUser user) {
        String businessKey = processInstance.getBusinessKey();
        BusPayment payment = paymentService.getById(businessKey);
        String amountType = payment.getAmountType();
        Object payBankId = completeVariables.get("payBankId");
        Object payDate = completeVariables.get("payDate");
        if (FlymeUtils.isNotEmpty(payment) && FlymeUtils.isNotEmpty(payBankId)) {
            payment.setPayBankId(Long.parseLong(payBankId.toString()));
            payment.setPayDate(DateUtils.strToDate(payDate.toString()));
            payment.setPayUserId(user.getUserId());
            payment.setPaymentState(CommonConstants.INT_2);
        }
    }

}
